/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.kz.shange.common;

import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.kz.shange.models.Author;

/**
 *
 * @author htkj
 */
public class KzTemplate {

    private Author loginUser;
    private int privilege;
    public static final String USER_LOGIN_OUTCOME = "/views/security/login.xhtml?faces-redirect=true&includeViewParams=true";
    public static String USER_SESSION_KEY = "kz.sg.luser";
    public static String USER_P_KEY = "kz.sg.p";    //权限
    public static int PV_VISITOR = 0;    //权限 游客
    public static int PV_MEMBER = 1;    //权限 注册账户
    public static int PV_MASTER = 2;    //权限 管理员

    public void addInfoMessage(String msg) {
        if (!msg.isEmpty()) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "消息", msg));
        }
    }

    public void addWarnMessage(String msg) {
        if (!msg.isEmpty()) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "警告", msg));
        }
    }

    public void addErrorMessage(String msg) {
        if (!msg.isEmpty()) {
            FacesContext.getCurrentInstance().validationFailed();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "错误", msg));
        }
    }

    public void addFatalMessage(String msg) {
        if (!msg.isEmpty()) {
        FacesContext.getCurrentInstance().validationFailed();
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "致命错误", msg));
        }
    }

    public boolean isValidateFailed() {        
        return FacesContext.getCurrentInstance().isValidationFailed();
    }

    public void saveLogonUserToSession(Author user) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.getExternalContext().getSessionMap().put(USER_SESSION_KEY, user);
    }

    public void removeLogonUserFromSession() {
        FacesContext context = FacesContext.getCurrentInstance();
        context.getExternalContext().getSessionMap().remove(USER_SESSION_KEY);
    }

    protected boolean securityValidate() {
        FacesContext context = FacesContext.getCurrentInstance();
        this.loginUser = (Author) context.getExternalContext().getSessionMap().get(USER_SESSION_KEY);
        if (this.loginUser == null) {
            context.responseComplete();
            context.getApplication().
                    getNavigationHandler().handleNavigation(context,
                            null,
                            USER_LOGIN_OUTCOME);
        }
        return this.loginUser != null;
    }

    protected String validateString(String valueName, String value, int maxLength) {

        if (value.isEmpty()) {
            return String.format("【%s】不能为空", valueName);
        }
        try {
            if (value.getBytes("GBK").length > maxLength) {
                return String.format("【%s】长度不能超过[%d]个字节", valueName, maxLength);
            }
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(KzTemplate.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "";
    }
// <editor-fold defaultstate="collapsed" desc="property">

    public Author getLoginUser() {
        FacesContext context = FacesContext.getCurrentInstance();
        Object o = context.getExternalContext().getSessionMap().get(USER_SESSION_KEY);
        this.loginUser = o == null ? null : (Author) o;
        return loginUser;
    }

    public int getPrivilege() {
        Author a = this.getLoginUser();
        this.privilege = a == null ? 0 : a.getPrivilege();

        return privilege;
    }

// </editor-fold>
}
